#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <err.h>
#include <libmscommon/mscommon.h>
#include <libmscommon/msmem.h>
#include <libmscommon/msepoll.h>
#include <libmslog/mslog.h>
#include <libmsprotocol/msprotocol.h>

#define FLAG "client"
/*
	{"control_cmd":"msifconfig","workmode":"nonework","iface_info":[{"name":"p2p1","address":"12:sdffsdfsdf","ipaddress":"192.168.0.222","netmask":"255.255.0.0","gateway":"192.168.0.1","mtu":"150","dns1":"dnndndd","dns2":"2222222","mode":"static"}]}
	{"control_cmd":"msifconfig","workmode":"nonework","iface_info":[{"name":"bond0","ipaddress":"192.168.0.222","netmask":"255.255.0.0","mode":"bond-master"},{"name":"p2p1","mode":"bond-slaver","bondmaster_name":"bond0"},{"name":"p6p1","mode":"bond-slaver","bondmaster_name":"bond0"}]}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"setup","ethn":"p2p1","user":"sugao_pppoe1","passwd":"123456"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"start","ethn":"p2p1"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"setup","ethn":"p6p1","user":"sugao_pppoe2","passwd":"123456"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"start","ethn":"p6p1"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"setup","ethn":"p6p2","user":"sugao_pppoe3","passwd":"123456"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"start","ethn":"p6p2"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"stop","ethn":"p6p1"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"status","ethn":"p6p1"}

	{"control_cmd":"mspppoe","workmode":"nonework","usage":"setup","ethn":"eth2","user":"sugao_pppoe2","passwd":"123456"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"start","ethn":"eth2"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"stop","ethn":"eth2"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"status","ethn":"eth2"}

	{"control_cmd":"mspppoe","workmode":"nonework","usage":"setup","ethn":"eth1","user":"sugao_pppoe3","passwd":"123456"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"start","ethn":"eth1"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"stop","ethn":"eth1"}
	{"control_cmd":"mspppoe","workmode":"nonework","usage":"status","ethn":"eth1"}

	{"control_cmd":"info"}
	{"control_cmd":"info","workmode":"transmit"}
	{"control_cmd":"info","task_id":1010}
	{"control_cmd":"get_state"}
	{"control_cmd":"read"}
	{"control_cmd":"clear_data"}
	{"control_cmd":"save"}
	{"control_cmd":"quit"}
	{"control_cmd":"setsysconfig","uxp_log_opt":53}
	{"control_cmd":"osinfo"}
	{"control_cmd":"rebootserver"}
	{"control_cmd":"poweroff"}
	{"control_cmd":"sysperforminfo"}
	{"control_cmd":"add","workmode":"streammonitor","listurlin":"rtp://192.168.1.244:8881"}
	{"control_cmd":"streaminfo","workmode":"nonework","listurlin":"http://192.168.200.199:8060/out1/out1.m3u8"}
	{"control_cmd":"keepalive","workmode":"nonework"}
	{"control_cmd":"serverversion"}
	{"control_cmd":"authorization"}
	{"control_cmd":"getsysconfig"}
	{"control_cmd":"add","workmode":"transmit","task_id":2001,"listurlin":"http://192.168.200.199:8060/out1/out1.m3u8","listurlout":"udp://127.0.0.1:4000","interfacein":"p2p1"}
	{"control_cmd":"add","workmode":"transmit","task_id":2002,"listurlin":"http://192.168.200.199:39100/ch1.ts","listurlout":"out_test.m3u8"}
	{"control_cmd":"delete","task_id":2002}
	{"control_cmd":"add","workmode":"transmit","task_id":1003,"listurlin":"udp://239.93.0.111:5140","listurlout":"/usr/local/nginx/html/out3.m3u8"}
	{"control_cmd":"add","workmode":"transmit","task_id":1004,"listurlin":"udp://239.93.0.71:5140","listurlout":"udp://127.0.0.1:2000","interfacein":"p2p1"}
	{"control_cmd":"add","workmode":"transmit","task_id":1007,"listurlin":"udp://239.93.0.70:5140","listurlout":"/usr/local/nginx/html/out7.m3u8"}
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"http://192.168.200.199:8010/ch1.ts","listurlout":"udp://127.0.0.1:3000"}
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"udp://224.2.2.2:3001","listurlout":"udp://224.2.2.2:2000"}
	{"control_cmd":"add","workmode":"dump_nor","task_id":123,"listurlin":"udp://224.3.3.2:10002"}
	{"control_cmd":"add","workmode":"transmit","task_id":3000,"listurlin":"file:///usr/local/mscore_web/upload/video/msatartup.mp3","listurlout":"udp://127.0.0.1:20000","interfaceout":"eth0","service_id":1230,"transport_stream_id":1240,"original_network_id":1250,"pmt_start_pid":1260,"mpegts_start_pid":1270}
	{"control_cmd":"delete","task_id":1007}
	{"control_cmd":"add","workmode":"streammonitor","listurlin":"udp://224.2.2.2:2000"}
	{"control_cmd":"add","workmode":"httpserver","listurlin":"ch1.ts-udp://224.2.2.2:3001"}
	{"control_cmd":"add","workmode":"transmit","listurlin":"http://192.168.200.199:39000/ch1.ts","listurlout":"out2.m3u8"}

	{"control_cmd":"delete","task_id":100}
	{"control_cmd":"add","workmode":"dump_timeshift_auto","task_id":1200,"pid":1200,"listurlin":"udp://239.93.0.112:5140"}
	{"control_cmd":"add","workmode":"dump_nor","listurlin":"udp://192.168.7.150:10001"}
	{"control_cmd":"add","workmode":"transmit","listurlin":"http://118.122.88.230:5460//luzhi1/vdj/live/c001/now/live.m3u8","listurlout":"udp://127.0.0.1:3000"}
	{"control_cmd":"add","workmode":"hrserver","task_id":100,"listurlin":"ch1.ts-udp://127.0.0.1:20000#ch2.ts-udp://127.0.0.1:20000","port":8010}
	{"control_cmd":"add","workmode":"srtserver","task_id":1000,"listurlin":"ch1.ts-udp://127.0.0.1:6001","port":8010}
	{"control_cmd":"delete","task_id":100}
	{"control_cmd":"add","workmode":"remux","listurlin":"rtmp://live.hkstv.hk.lxdns.com/live/hks","listurlout":"udp://127.0.0.1:3000"}
	{"control_cmd":"add","workmode":"remux","listurlin":"udp://224.2.2.2:3000","listurlout":"/usr/local/nginx/html/1.m3u8"}
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"udp://127.0.0.1:2003","listurlout":"udp://192.168.200.244:10000","task_id":100}
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"udp://224.2.2.2:2001","listurlout":"udp://127.0.0.1:3001"}
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"udp://224.2.2.2:2002","listurlout":"udp://127.0.0.1:3002"}

	
	{"control_cmd":"add","workmode":"transcodevideo","listurlin":"udp://224.2.2.2:3000","listurlout":"udp://224.2.2.3:5000","drawtext":{"enable":1,"displaypostion":1,"x":0,"y":500,"speed":40,"displaymode":0,"count":3,"interval":240,"valid_date":{"start_date":"2017-04-13","end_date":"2017-05-13","valid_time":[{"start_time":"00:00","end_time":"00:12"}],"opt":1},"box":{"enable":1,"boxborderw":0,"boxcolor":"black","line_spacing":0,"borderw":0,"bordercolor":"black"},"text":{"fontcolor":"white","font":"Sans","alpha":1,"fontsize":20,"shadowcolor":"black","shadowx":0,"shadowy":0,"content":"hello,world"}},"overlay":[{"index":0,"src":"/etc/mscore/mscore_logo.png","isscale_src":0,"forcew":100,"forceh":100,"displaypostion":0,"x":10,"y":10,"displaymode":0,"count":3,"show_duration":20,"interval":240,"valid_date":{"start_date":"2017-04-13","end_date":"2017-05-13","valid_time":[{"start_time":"00:00","end_time":"00:12"}],"opt":1}}],"videoenc":{"preset":"fast","profile":"main","level":"auto","refs":3,"gop_size":25,"max_b_frames":4,"force_idr":0,"rc":"vbr","bit_rate":2097152,"rc_buffer_size":2097152,"rc_min_rate":2097152,"rc_max_rate":2097152,"width":-1,"height":-1}}
	{"control_cmd":"add","workmode":"hrserver","task_id":1200,"port":8013,"listurlin":"ch1-udp://224.2.2.2:3000#ch2-udp://224.2.2.2:3004#ch3-udp://224.2.2.2:3008#ch4-udp://224.2.2.2:3012#ch5-udp://224.2.2.2:3016#ch6-udp://224.2.2.2:3020"}
	{"control_cmd":"delete","workmode":"hrserver","task_id":1200}

	{"control_cmd":"setauthorization","workmode":"nonework","authent_envcodecid":1,"authent_envcodectype":1}
	
	{"control_cmd":"add","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch2222.ts-udp://224.2.2.2:12343333"}
	{"control_cmd":"add","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch3.ts-udp://224.2.2.2:12343333"}
	{"control_cmd":"add","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch4.ts-udp://224.2.2.2:1234"}
	{"control_cmd":"delete","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch111.ts-udp://224.2.2.2:1234"}
	{"control_cmd":"delete","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch2222.ts-udp://224.2.2.2:12343333"}
	{"control_cmd":"delete","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch3.ts-udp://224.2.2.2:12343333"}
	{"control_cmd":"delete","workmode":"hrserver","task_id":100,"port":8013,"listurlin":"ch4.ts-udp://224.2.2.2:1234"}
	{"control_cmd":"add","workmode":"streammonitor","listurlin":"udp://224.2.2.2:2000"}	
	{"control_cmd":"add","workmode":"dump_nor","listurlin":"udp://192.168.7.150:10001","programname":"mscore001","platform_id":1,"task_id":101,"tasktime":"B20190731104337E20190731134337"}
	{"control_cmd":"add","workmode":"transmit","listurlin":"rtsp://111.48.37.88:554/HBUT_RTSP/00000001000000050000000000000058?Type=tv&UserID=F7163936717B01","listurlout":"udp://127.0.0.1:3000","opt":"usefifo"}
{"control_cmd":"add","task_id":100,"workmode":"transmit","listurlin":"http://127.0.0.1:8060/mscore/av/msatartup.mp3","listurlout":"rtp://127.0.0.1:3000","opt":"vodfile&novideo"}
{"control_cmd":"add","task_id":101,"workmode":"transmit","listurlin":"http://127.0.0.1:8060/mscore/av/msatartup.mp3","listurlout":"rtp://127.0.0.1:3000","opt":"vodfile&novideo"}
{"control_cmd":"add","task_id":102,"workmode":"transmit","listurlin":"http://127.0.0.1:8060/mscore/av/msatartup.mp3","listurlout":"rtp://127.0.0.1:3000","opt":"vodfile&novideo"}
*/
#if 1
int main(int argc, char **argv)
{
	mslog_api_setopt(21,ms_null,0);
	msmem_api_init(ms_false);
	msprotocol_api_init();
	register_protocol();

	URLProtocol *mstcp_ptl=msprotocol_api_match(PTLNAME_TCP);
	URLProtocol *msout_ptl=msprotocol_api_match(PTLNAME_UDP);
	URLProtocol *msptl=NULL;

	URLContext cilent_ctt;
	memset(&cilent_ctt,0,sizeof(URLContext));
	
	int ptltype=0;
	if(argc==3){
		ms_strcpy(cilent_ctt.url_info.hostname,argv[1]);
		cilent_ctt.url_info.port=atoi(argv[2]);
	}else{
		ms_waring("Use deafult val: %s 127.0.0.1 50000",argv[0]);
		ms_strcpy(cilent_ctt.url_info.hostname,"127.0.0.1");
		cilent_ctt.url_info.port=50000;
	}
	ms_debug("hostname:%s,prot:%d",cilent_ctt.url_info.hostname,cilent_ctt.url_info.port);
	ms_info("Please input protocol-type (0-tcp  | 1-udp):"); 		   
	scanf("%d",&ptltype);
	switch(ptltype){
		case 0:
			msptl=mstcp_ptl;
			ms_sprintfs(cilent_ctt.url,"tcp://%s:%d",cilent_ctt.url_info.hostname,cilent_ctt.url_info.port);
			break;
		case 1:
			cilent_ctt.flags|=FLAG_WRITE;
			ms_sprintfs(cilent_ctt.url,"udp://%s:%d",cilent_ctt.url_info.hostname,cilent_ctt.url_info.port);
			msptl=msout_ptl;
			break;
	}
	msptl->url_open(&cilent_ctt);
	int mctu_msepoll=-1;
	if( (mctu_msepoll=msepoll_open(2))<0	){
		ms_errexit(-1,"[Error] Cms msepoll_open:%s failed",cilent_ctt.url);
	}
	msepoll_add(mctu_msepoll,cilent_ctt.fd,msepollin);
	msepoll_event cms_event[2];
	char databuf[1024*100];
	while(1){
		ms_info("**********************************");
		ms_info("*  Input cmd    ");
		ms_info("**********************************");
		scanf("%s",databuf);
		int len=msptl->url_write(&cilent_ctt,databuf,strlen(databuf));
		ms_memset(databuf,0,sizeof(databuf));
		while(1){		
			memset(cms_event,0,sizeof(msepoll_event));
			int event_num=msepoll_wait(mctu_msepoll,(msepoll_event *)cms_event,2);
			if(event_num>0){
				int event_index=0;
				for(event_index=0;event_index<event_num;event_index++){
					if(cms_event[event_index].events&msepollhup){
						ms_debug("cms-msepollhup");
					}else if(cms_event[event_index].events&msepollerr){
						ms_debug("cms-msepollerr");
					}else if(cms_event[event_index].events&msepollin){
						len=msptl->url_read(&cilent_ctt,databuf,sizeof(databuf));
						if(len>0){
							ms_info("=====>Reply info:\r\n%s",databuf);
						}else{
							ms_info("=====>Reply info:no data");
						}
						break;
					}
				}		
			}else{
				ms_info("=====>Reply info:no data");
			}
			break;
		}
	}	
	msptl->url_close(&cilent_ctt);
	msepoll_close(&mctu_msepoll);
end:	
	msprotocol_api_deinit();
	msmem_api_deinit();
	mslog_api_unsetopt();
}
#elif 0
int main(int argc, char **argv)
{
	mslog_api_setopt(53,ms_null,0);
	register_protocol();

	URLProtocol *msin_ptl=msprotocol_api_match(PTLNAME_FILE);
	URLProtocol *msout_ptl=msprotocol_api_match(PTLNAME_UDP);
	URLContext in_ctt;
	URLContext out_ctt;
	ms_memset(&in_ctt,0,sizeof(URLContext));
	ms_memset(&out_ctt,0,sizeof(URLContext));
	ms_strcpy(in_ctt.url, "file:///home/sugao/123.ts");
	ms_strcpy(out_ctt.url, "udp://224.2.2.2:3000");
	out_ctt.urlctt_opt.udp_opt.interfaceout="lo";
	out_ctt.urlctt_opt.udp_opt.localhostipout="127.0.0.1";
	out_ctt.flags|=FLAG_WRITE;
	msin_ptl->url_open(&in_ctt);
	msout_ptl->url_open(&out_ctt);
	char buf[2048]={0};
	int len=0;
	while(1){
		len=msin_ptl->url_read(&in_ctt,buf,1328);

		ms_debug("read===%d",len);
		
		msout_ptl->url_write(&out_ctt,buf,len);
		
		//ms_msleep(100);
	}	
	msin_ptl->url_close(&in_ctt);
	msout_ptl->url_close(&out_ctt);
	
	mslog_api_unsetopt();
}
#else
int main(int argc, char **argv)
{
	mslog_api_setopt(53,ms_null,0);
	msprotocol_api_init();
	register_protocol();
srt_setloglevel(4);
	URLProtocol *msin_ptl=msprotocol_api_match(PTLNAME_SRT);
	URLProtocol *msout_ptl=msprotocol_api_match(PTLNAME_UDP);
	URLContext in_ctt;
	URLContext out_ctt;
	ms_memset(&in_ctt,0,sizeof(URLContext));
	ms_memset(&out_ctt,0,sizeof(URLContext));
	ms_strcpy(in_ctt.url, "srt://127.0.0.1:9000");
	ms_strcpy(out_ctt.url, "udp://192.168.200.244:6000");
	out_ctt.urlctt_opt.udp_opt.interfaceout="lo";
	out_ctt.urlctt_opt.udp_opt.localhostipout="127.0.0.1";
	out_ctt.flags|=FLAG_WRITE;
	if(msin_ptl->url_open(&in_ctt)<0){
		ms_errexit(-1, "Open in url failed");
	}
	
	out_ctt.urlctt_opt.srt_opt.listen=1;
	if(msout_ptl->url_open(&out_ctt)<0){
		ms_errexit(-1, "Open out url failed");
	}
	char buf[2048]={0};
	int len=0;
	while(1){
		len=msin_ptl->url_read(&in_ctt,buf,1316);
		
		msout_ptl->url_write(&out_ctt,buf,len);
	}	
	msin_ptl->url_close(&in_ctt);
	msout_ptl->url_close(&out_ctt);
	msprotocol_api_deinit();
	mslog_api_unsetopt();
}
#endif
#undef MAIN_C

